package com.lovo.Atm;

import com.lovo.Stream.SerializeUtil;
import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class Home extends JFrame {
    private ArrayList user;
    private Land landPanel = new Land(this);
    private Menu menuPanel = new Menu(this);
    private Get getPanel = new Get(this);
    private Save savePanel = new Save(this);
    private Find findPanel = new Find(this);
    private Transfer transferPanel = new Transfer(this);
    private Mod modPanel = new Mod(this);
    private CardLayout cardLayout = new CardLayout();
    private Image bg = Util.image("/images/background.jpg");
    //登录成功用户对象
    private User loginUser;
    //路径常量
    public static final String path = "c:\\learn\\testui\\atm.dat";

    public User getLoginUser() {
        return loginUser;
    }

    public Find getFindPanel() {
        return findPanel;
    }

    public Home() {
        init();
        this.setContentPane(new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bg, 0, 0, 1024, 768, this);
            }
        });
        this.setLayout(cardLayout);
        this.add("land", landPanel);
        this.add("menu", menuPanel);
        this.add("get", getPanel);
        this.add("save", savePanel);
        this.add("find", findPanel);
        this.add("transfer", transferPanel);
        this.add("mod", modPanel);
        this.setSize(1024, 768);
        this.setLocationRelativeTo(null);
        this.setTitle("ATM");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    //初始化用户数据方法 链接数据库 添加用户数据
    public void init() {
//        user = (ArrayList<User>) SerializeUtil.readObject(path);
//        if (user == null) {
//            user = new ArrayList<>();
//            user.add(new User("aaa", "111111", 1000));
//            user.add(new User("bbb", "222222", 3000));
//            user.add(new User("ccc", "333333", 4000));
//            user.add(new User("ddd", "444444", 5000));
//            user.add(new User("eee", "666666", 6000));
//            SerializeUtil.writeObject(path,user);
//            String sql = "INSERT INTO t_user(u_name,u_password,u_blance) values('aaa', '111111',1000),('eee','555555',6000),('ccc','333333',5000),('bbb','222222',10000),('ddd','444444',8000)";
//            user = JdbcUtil.revise(sql);
//        }
        user = JdbcUtil.query();
    }

    public ArrayList<User> getUser() {
        return user;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    public void switchPanel(String name) {
        cardLayout.show(this.getContentPane(), name);
    }

    public Transfer getTransferPanel() {
        return transferPanel;
    }

    public void setTransferPanel(Transfer transferPanel) {
        this.transferPanel = transferPanel;
    }

    public static void main(String[] args) {
        new Home();
    }
}
